<%
layout("/layouts/platform.html"){
%>
	<div class="panel-body" style="padding-top: 15px;">
		<div class="box">
			<div class="box-body">
				<div class="col-sm-12">
					<div class="row">
				        <table class="table table-bordered table-hover mg-t datatable">
			                <thead>
			                <tr>
			                    <th>发放主体</th>
			                    <th>卡券类型</th>
			                    <th>卡券名称</th>
			                    <th>来源</th>
			                    <th>原始ID</th>
			                    <th>状态</th>
			                    <th>操作</th>
     		        	</table>
	    			</div>
				</div>
			</div>
		</div>
	</div>
<!-- 修改-->
	<div id="editModal" class="modal fade bs-modal-sm"   tabindex="-3" role="dialog" aria-hidden="true">
	    <div id="editModalDialog" class="modal-dialog " >
	        <div class="modal-content">
	        
	        </div>
	    </div>
	</div>
<script language="JavaScript">
    var datatable;
    function initDatatable() {
        datatable = $('.datatable').DataTable({
            "dom": '<"toolbar">frtip',
            "searching":false,
            "processing": true,
            "serverSide": true,
            "select": true,
            "ordering": true,
            "language": {
                "url": "${base}/assets/plugins/datatables/cn.json"
            },
            "preDrawCallback": function () {
                sublime.showLoadingbar($(".main-content"));
            },
            "drawCallback": function () {
                sublime.closeLoadingbar($(".main-content"));
            },
            "ajax": {
                "url": "${base}/platform/card/data",
                "type": "post",
                "data": function (d) {
                    d.title = $('#title').val();
                }
            },
            "order": [[0, "desc"]],
            "columns": [
                {"data": "company_id", "bSortable": true},
                {"data": "type", "bSortable": true},
                {"data": "rawid", "bSortable": true},
                {"data": "source", "bSortable": true},
                {"data": "rawid", "bSortable": true},
                {"data": "state", "bSortable": true}
            ],
            "columnDefs": [
                {
                    "render": function (data, type, row) {
                    	var html='<div>';
                        if(row.state==0){
                        	html+='<a class="btn btn-xs btn btn-default" href="${base}/platform/card/putOn/' + row.id + '" data-toggle="modal" data-target="#editModal" ><i class="fa fa-edit"></i>&nbsp;投放</a>';
                        }
                        html+='</div>';
                        return  html;
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row) {
                    	if(data==1){
                    		return "已投放";
                    	}else if(data==0){
                    		return "未上架";
                    	}else{
                    		return "已下架";
                    	}
                    },
                    "targets": 5
                }
                ,
                {
                    "render": function (data, type, row) {
                    	if(data=="GROUPON"){
                    		return "团购券";
                    	}else if(data=="CASH"){
                    		return "代金券";
                    	}else if(data="DISCOUNT"){
                    		return "折扣券";
                    	}else if(data="GIFT"){
                    		return "兑换券";
                    	}else if(data="GENERAL_COUPON"){
                    		return "优惠券";
                    	}
                    },
                    "targets": 1
                },
                {
                	"render": function (data, type, row) {
                       	return getCardName(data,row.source);
                   	 },
                    "targets": 2
              	},
                {
                	"render": function (data, type, row) {
                       	return getCompanyName(data);
                   	 },
                    "targets": 0
              	}
            ]
        });
    }
    $(document).ready(function () {
        initDatatable();
        
        $("#editModal").on("hidden.bs.modal", function () {
            $(this).removeData("bs.modal");
        });
    });
    
    function getCompanyName(id){
    	var datas="";
    	$.ajax({
			url : "${base}/platform/card/getCompanyName",
			data : {
				id : id
			},
			type : "post",
			async : false,
			dataType : "json",
			success : function(data){
				datas= data;
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				return false;
			}
		});
    	return datas;
    }
    
    function getCardName(rawid,source){
    	var datas="";
    	$.ajax({
			url : "${base}/platform/card/getCardName",
			data : {
				rawid : rawid,
				source:source
			},
			type : "post",
			async : false,
			dataType : "json",
			success : function(data){
				datas= data;
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				return false;
			}
		});
    	return datas;
    }
</script>
<%}%>
